package com.loadmore.common.util;


import android.text.TextUtils;

import com.alibaba.fastjson.JSON;

import java.util.List;

/**
 * @author by sunleilei
 * @date 2017/9/26
 * @time 下午9:02
 */

public class JsonUtil {
    public static <T> T getJson(String data, Class<T> clazz) {
        if (isJson(data)) {
            T t = JSON.parseObject(data,clazz);
            return t;
        }
        return null;
    }


    public static <T> List<T> getJsonList(String data,Class<T> clazz) {
        if (isJsonArray(data)) {
            List<T> t = JSON.parseArray(data,clazz);
            return t;
        }
        return null;
    }

    public static boolean isJson(String string) {
        return !isEmpty(string) && string.startsWith("{") && string.endsWith("}");
    }

    public static boolean isJsonArray(String string) {
        return !isEmpty(string) && string.startsWith("[") && string.endsWith("]");
    }

    public static boolean isEmpty(String string) {
        return TextUtils.isEmpty(string) || TextUtils.equals(string,"null") || TextUtils.equals(string,"NULL");
    }
}
